package com.young.android.atomoperation;

import java.util.ArrayList;

import android.database.Cursor;

import com.young.android.common.Const;
import com.young.android.common.PubFunction;
import com.young.android.dbreposit.SqliteHelper;
import com.young.android.entity.Item;
import com.young.android.entity.Resource;

public class OperItem {

    /**
     * 插入
     * 
     * @param i
     * @return
     */
    public static int insertItem(Item it) {

        StringBuffer sb = new StringBuffer();
        sb.append("INSERT INTO ITEM_DB( ");
        sb.append("TITLE,          ");
        sb.append("LINK,           ");
        sb.append("DESCRIPTION,    ");
        sb.append("DATE,           ");
        sb.append("BELONG,         ");
        sb.append("STATE)");
        sb.append(" VALUES( ");
        sb.append("'" + it.title + "',");
        sb.append("'" + it.link + "',");
        sb.append("'" + it.description + "',");
        sb.append("'" + it.date + "',");
        sb.append("'" + it.belong + "',");
        sb.append("'" + it.state + "'");
        sb.append(" ) ");

        return SqliteHelper.ExecSQL(sb.toString());

    }

    /**
     * 修改 （仅限修改状态）
     * 
     * @param r
     * @param name
     * @return
     */
    public static int updateItem(Item i, int state) {

        StringBuffer sb = new StringBuffer();
        sb.append("UPDATE ITEM_DB SET ");
        sb.append("STATE  =" + state );
        sb.append(" WHERE TITLE ='" + i.title + "'");
        sb.append(" AND BELONG  ='" + i.belong + "'");

        return SqliteHelper.ExecSQL(sb.toString());

    }

    /**
     * 删除 仅限删除某URL下所有条目
     * 
     * @param r
     * @return
     */
    public static int deleteItem(String belong) {
        StringBuffer sb = new StringBuffer();
        sb.append("DELETE FROM ITEM_DB ");
        sb.append(" WHERE BELONG='" + belong + "'");

        return SqliteHelper.ExecSQL(sb.toString());
    }

    /**
     * 删除最后记录
     * 
     * @param num
     * @return
     */
    public static int deleteLastItem(int num) {
        StringBuffer sb = new StringBuffer();
        sb.append("DELETE FROM ITEM_DB ");
        sb.append(" WHERE _id=(SELECT TOP) ");
        sb.append(num + " _id from ITEM_DB ORDER BY _id DESC)");

        return SqliteHelper.ExecSQL(sb.toString());
    }

    /**
     * 查询是否存在
     * 
     * @param i
     * @return
     */
    public static boolean isExist(Item i) {
        boolean breturn = false;
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT _id FROM ITEM_DB ");
        sb.append(" WHERE TITLE ='" + i.title + "'");
        sb.append(" AND  BELONG ='" + i.belong + "'");

        Cursor cur = SqliteHelper.QuerySql(sb.toString());
        if ((cur != null) && (cur.getCount() > 0)) {
            breturn = true;
        }
        PubFunction.CloseCursor(cur);

        return breturn;
    }

    /**
     * 获取部分记录
     * 
     * @param num
     *            获取数量
     * @param belong
     *            所属URL
     * @param position
     *            跳过条数(用户获取更多)
     * @return
     */
    public static ArrayList<Item> getItems(int num, String belong, int position) {
        Item i = null;
        ArrayList<Item> items = new ArrayList<Item>();
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT TITLE,   ");
        sb.append(" LINK,          ");
        sb.append(" DESCRIPTION,   ");
        sb.append(" DATE,          ");
        sb.append(" BELONG,        ");
        sb.append(" STATE          ");
        sb.append(" FROM ITEM_DB   ");
        sb.append("WHERE BELONG ='" + belong + "'");
        sb.append(" ORDER BY DATE DESC "); // 日期排列
        sb.append(" limit " + position + "," + (position + num)); // 限制条数

        Cursor cur = SqliteHelper.QuerySql(sb.toString());
        try {
            if ((cur != null) && (cur.getCount() > 0)) {
                cur.moveToFirst();
                do {
                    i = new Item();
                    i.LoadFormCursor(cur);
                    items.add(i);
                } while (cur.moveToNext());
            }
        } finally {
            PubFunction.CloseCursor(cur);
        }

        return items;
    }

    /**
     * 获取某URL未独条目数量
     * 
     * @param belong
     * @return
     */
    public static int getUnreadCount(String belong) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT _id FROM ITEM_DB ");
        sb.append(" WHERE STATE ='" + Const.ITEM_UNREAD + "'");
        sb.append(" AND  BELONG ='" + belong + "'");

        Cursor cur = SqliteHelper.QuerySql(sb.toString());
        int ireturn = cur.getCount();
        PubFunction.CloseCursor(cur);
        return ireturn;
    }

}
